package client;

import java.io.File;
import java.io.FileNotFoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;

import util.MD5Checksum;
import util.PolicyLoader;
import util.VersiRemote;
import client.ftp.FTPImplementation;
import util.pref.PrefHandler;

public class ClientHandler {
	
	/**
	 * This class is responsible for establishing a connection
	 * with a server and processing user input. It contains three
	 * methods which are responsible for handling RMI communication.
	 */
	
private FTPImplementation ftp;
private VersiRemote vr;

String[] s=new String[5];
private PrefHandler ph=new PrefHandler();
HashMap h;

/**
 * Initialize a client side RMI connection
 * @throws RemoteException
 * @throws NotBoundException
 */

public ClientHandler() throws RemoteException, NotBoundException {
	
	s[0]="HOSTNAME";
	s[1]="PORT";
	s[2]="USERNAME";
	s[3]="REMOTEFOLDER";
	s[4]="LOCALFOLDER";
	h= ph.getPreferences(s);
	

	System.setProperty("java.security.policy", PolicyLoader.getLocationOfPolicyFile());
	
	if(System.getSecurityManager() == null) System.setSecurityManager(new SecurityManager());
		Registry registry = LocateRegistry.getRegistry((String)h.get("HOSTNAME"), 1099);
		vr=(VersiRemote)registry.lookup("VersiRemote");
		
		
	ftp=new FTPImplementation();
	ftp.loadSettings();
}


/**
 * Perform a check out by downloading a selected file and locking it on the server
 * @param filePath the local path to the file
 * @return true if the check out process is complete or false if the process fails
 */
public int checkOut(String file) throws RemoteException, Exception {
    //Check file paths - get path from preferences
	String name = new File(file).getName();
	if(ftp.download("remotefileName")==false) return 0;
	if(vr.checkout(name, (String)h.get("REMOTEFOLDER"))==false) return 0;
	else return 0;
}
/**
 * Perform a check in by uploading a selected file and unlocking it on the server
 * @param filePath the local path to the file
 * @return true if the check out process is complete or false if the process fails
 */
public boolean checkIn(String filePath) throws RemoteException, FileNotFoundException, Exception {
	String name = new File(filePath).getName();
	if(vr.checkForConflict(name, MD5Checksum.getMD5Checksum(name), new File(filePath).lastModified())==3) return false;
	else if(vr.checkin(name, (String)h.get("USERNAME"))==false) return false;
	else if(ftp.upload(name)==false) return false;
	else return true;
}
/**
 * Test method please ignore
 * @throws RemoteException
 */
public void testMethodPleaseIgnore() throws RemoteException {
	System.out.println(vr.testMethodPleaseIgnore());
}
/**
 * ftp library upload method test

 */
public void ftpTestPleaseIgnore() {
	System.out.println("Starting upload...");
	ftp.upload((String)h.get("LOCALFOLDER")+"stuff_please_ignore.png");
	System.out.println("Upload Compete...");
	
}

/**
 * ftp library download method test
 */
public void anotherFtpTestPleaseIgnore() {
	System.out.println("Starting download...");
	ftp.download((String)h.get("REMOTEFOLDER")+"more_stuff_please_ignore.png");
	System.out.println("Download Compete...");
}
}
